<html>
<head>

<title>Groovy Goodness: Store Closures in Script Binding</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Store Closures in Script Binding</h3>

<div class="post">
<p>We have several ways to run (external) Groovy scripts from our code. For example we can use <code>GroovyShell</code> and the <code>evaluate()</code> method. If we want to pass variables to the script to be run we must create a <code>Binding</code> object and assign values to the variables and in our script we can use the variables. But if we assign closures to the variables we have methods available in our scripts. We can use it for example to define a DSL externally from the script that uses the DSL.</p>
<p>First we create a script with a simple DSL for sending text to the console or to a file:</p>
<pre class="brush:groovy">
// File: Output.groovy
console 'Groovy is great'

file('result.txt') { out -&gt;
    out &lt;&lt; 'Yes it is!'
}
</pre>
<p>Now we define the Groovy script that reads the script file and executes it:</p>
<pre class="brush:groovy">
// File: RunOutput.groovy
// Define DSL methods as closure variables in the binding.
def binding = new Binding()
binding.console = { String message ->
    println message
}
binding.file = { String fileName, Closure outputCode ->
    def outputFile = new File(fileName)
    outputFile.withWriter { writer ->
        outputCode.call writer
    }
}

// Create shell with the new binding.
def shell = new GroovyShell(binding)
def script = new File('Output.groovy')

// And execute
shell.evaluate script
</pre>
<p>We get the following output when we run the script:</p>
<pre class="brush:plain">
$ groovy RunOutput
Groovy is great
</pre>
<p>And in the current directory we have a new file <code>result.txt</code> with the following contents: <code>Yes it is!</code>.</p
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>